﻿using DryIoc;
using Prism.DryIoc;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using WPFPrism.Common;
using WPFPrism.Service;
using WPFPrism.ViewModels;
using WPFPrism.ViewModels.Dialogs;
using WPFPrism.Views;
using WPFPrism.Views.Dialogs;

namespace WPFPrism
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        protected override Window CreateShell()
        {
            //通过容器去拿MainView,也是启动的地方000
            return Container.Resolve<MainView>();
        }
        public static void LoginOut(IContainerProvider containerProvider)
        {
            Current.MainWindow.Hide();

            var dialog = containerProvider.Resolve<IDialogService>();

            dialog.ShowDialog("LoginView", callback =>
            {
                if (callback.Result != ButtonResult.OK)
                {
                    Environment.Exit(0);
                    return;
                }

                Current.MainWindow.Show();
            });
        }
        protected override void OnInitialized()
        {
            var dialog = Container.Resolve<IDialogService>();

            dialog.ShowDialog("LoginView", callback =>
            {
                if (callback.Result != ButtonResult.OK)
                {
                    Environment.Exit(0);
                    return;
                }

                var service = App.Current.MainWindow.DataContext as IConfigureService;
                if (service != null)
                    service.Configure();
                base.OnInitialized();
            });
        }
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            //依赖注入的功能，增加依赖注入要实现的内容
            //containerRegistry.RegisterForNavigation<ViewA>(); //containerRegistry.RegisterForNavigation<ViewA>("ViewA的具体名字")自定义，可以这样写;

            containerRegistry.GetContainer()
               .Register<HttpRestClient>(made: Parameters.Of.Type<string>(serviceKey: "webUrl"));
            containerRegistry.GetContainer().RegisterInstance(@"http://localhost:28772/", serviceKey: "webUrl");

            containerRegistry.Register<ILoginService, LoginService>();
            containerRegistry.Register<IToDoService, ToDoService>();
            containerRegistry.Register<IMemoService, MemoService>();
            containerRegistry.Register<IDialogHostService, DialogHostService>();

            containerRegistry.RegisterDialog<LoginView, LoginViewModel>();

            containerRegistry.RegisterForNavigation<AddToDoView, AddToDoViewModel>();
            containerRegistry.RegisterForNavigation<AddMemoView, AddMemoViewModel>();

            containerRegistry.RegisterForNavigation<IndexView, IndexViewModel>();
            containerRegistry.RegisterForNavigation<MemoView, MemoViewModel>();
            containerRegistry.RegisterForNavigation<SettingsView, SettingsViewModel>();
            containerRegistry.RegisterForNavigation<ToDoView, ToDoViewModel>();

            containerRegistry.RegisterForNavigation<SkinView, SkinViewModel>();
            containerRegistry.RegisterForNavigation<AboutView, AboutViewModel>();
            containerRegistry.RegisterForNavigation<MsgView, MsgViewModel>();
            containerRegistry.RegisterForNavigation<SystemConfigView, SystemConfigViewModel>();
            


            //注册弹框提示的界面
        }

        protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
        {
            //这里是走代码的
            //moduleCatalog.AddModule<ModuleAfile>();//加载A模块
            //moduleCatalog.AddModule<ModuleBfile>();//加载B模块
            base.ConfigureModuleCatalog(moduleCatalog);
        }
        protected override IModuleCatalog CreateModuleCatalog()
        {
            //这里是走配置的
            //return new DirectoryModuleCatalog()
            //{
            //    ModulePath = @"Modules"//这里是主程序exe同文件夹下面的Modules文件夹
            //};
            return base.CreateModuleCatalog();
        }

    }
}
